//
//  MovieParticularsViewController.m
//  Fengly_Nuo_Movie
//
//  Created by dllo on 16/1/1.
//  Copyright © 2016年 Fengly_Nuo. All rights reserved.
//

#import "MovieParticularsViewController.h"
#import "UIImageView+WebCache.h"
#import "Fengly_nuo_SQLDataBaseHandle.h"

// MARK:VIEW_CENTER_X:屏中心x轴
#define VIEW_CENTER_X self.view.center.x

// MARK:VIEW_CENTER_Y:屏中心y轴
#define VIEW_CENTER_Y self.view.center.y

// MARK:HEIGHT:屏高
#define HEIGHT self.view.frame.size.height

// MARK:WIDTH:屏宽
#define WIDTH self.view.frame.size.width

#define kBigViewWidth bigView.frame.size.width
#define kBigViewHeight bigView.frame.size.height

@interface MovieParticularsViewController ()<UIWebViewDelegate>

@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UIWebView *webView;
@property (nonatomic, retain) UIView *myView;
@property (nonatomic, retain) UIActivityIndicatorView *activity;

@end

@implementation MovieParticularsViewController

- (void)dealloc {
    [_activity release];
    [_myView release];
    [_model release];
    [_webView release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self initWebView];
    
    // 主页面布局
    [self createMPView];
//    // 页面布局
//    [self createAllInfoView];
    
    NSLog(@"%@", _model.url);
}



#pragma mark - WebView Delegate
// 开始加载
- (void)webViewDidStartLoad:(UIWebView *)webView {
    [self initView];
    
    [_activity startAnimating];
}

// 加载完成
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [_myView removeFromSuperview];
}

// 加载失败
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    
}



- (void)initWebView {
    self.webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
//    NSString *urlStr = _model.url;
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]]];
    [self.view addSubview:_webView];
    [_webView release];
    
    
    
    _webView.delegate = self;
}

- (void)initView {
    self.myView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:_myView];
    _myView.backgroundColor = [UIColor lightGrayColor];
    _myView.alpha = 0.5;
    [_myView release];
    
    self.activity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
    [_myView addSubview:_activity];
    _activity.center = _myView.center;
    [_activity release];
}


// 主页面布局
- (void)createMPView {
//    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = _model.title;
    
    // right bar button item
    UIBarButtonItem *collectButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_share.png"] style:(UIBarButtonItemStylePlain) target:self action:@selector(collectButtonItemAction:)];
    self.navigationItem.rightBarButtonItem = collectButtonItem;
    
    // left bar button item
    UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_back.png"] style:(UIBarButtonItemStylePlain) target:self action:@selector(backButtonItemAction:)];
    self.navigationItem.leftBarButtonItem = backButtonItem;
    
//    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
//    [self.view addSubview:_scrollView];
//    [_scrollView release];
//    _scrollView.contentSize = CGSizeMake(0, HEIGHT * 2);
//    _scrollView.showsVerticalScrollIndicator = NO;
}

// 页面布局
- (void)createAllInfoView {
    UIView *bigView = [[UIView alloc] initWithFrame:CGRectMake(25, 30, WIDTH - 50, HEIGHT / 2.8)];
    [_scrollView addSubview:bigView];
    [bigView release];
//    bigView.backgroundColor = [UIColor redColor];
    
    UIImageView *movieImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1.1 * kBigViewWidth / 3, 3 * kBigViewHeight / 4)];
    [movieImage sd_setImageWithURL:[NSURL URLWithString:_model.image]];
    [bigView addSubview:movieImage];
    [movieImage release];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 3 * kBigViewHeight / 4 + 1, kBigViewWidth, 2)];
    [bigView addSubview:lineView];
    [lineView release];
    lineView.backgroundColor = [UIColor grayColor];
}


- (void)collectButtonItemAction:(UIBarButtonItem *)buttonItem {
    
    NSArray *arrCollectMovies = [[Fengly_nuo_SQLDataBaseHandle sharedDataBaseHandle] selectMovieTable];

    // 当数组存在该元素时
    if ([arrCollectMovies containsObject:_model.title]) {
        UIAlertController *hadCollectAlert = [UIAlertController alertControllerWithTitle:@"收藏提示" message:@"该活动已经被收藏过" preferredStyle:(UIAlertControllerStyleAlert)];
        [self presentViewController:hadCollectAlert animated:YES completion:nil];
        [self performSelector:@selector(time) withObject:self afterDelay:0.5];
        
        // 不存在
    } else if (![arrCollectMovies containsObject:_model.title]) {
        [[Fengly_nuo_SQLDataBaseHandle sharedDataBaseHandle] insertMovieModel:_model];
        UIAlertController *collectAlert = [UIAlertController alertControllerWithTitle:@"收藏提示" message:@"收藏成功" preferredStyle:(UIAlertControllerStyleAlert)];
        [self presentViewController:collectAlert animated:YES completion:nil];
        [self performSelector:@selector(time) withObject:self afterDelay:0.5];
    }
}

- (void)time {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)backButtonItemAction:(UIBarButtonItem *)buttonItem {
    [self.navigationController popToRootViewControllerAnimated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
